package com.wideplay.warp.clustering.oscache;

import com.wideplay.warp.clustering.DataGridService;
import com.wideplay.warp.clustering.GridScoped;
import com.google.inject.Injector;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

import java.lang.annotation.Annotation;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * On: 23/06/2007
 *
 * @author Dhanji R. Prasanna
 * @since 1.0
 */
class OsCacheDataGridService extends DataGridService {
    private final Injector injector;

    @Inject
    public OsCacheDataGridService(Injector injector, SpacesListWrapper wrapper) {
        super(wrapper.getSpaceNames());

        this.injector = injector;
    }

    //TODO set properties from cache.properties
    public synchronized void start() {
        if (spaceNames.isEmpty()) {
            createSpace(GridScoped.class, null);
        } else {
            //register all caches as necessary (configs should be bound at appropriate annotation keys)
            for (Class<? extends Annotation> spaceName : super.spaceNames) {
                createSpace(spaceName, null);
            }
        }
    }

    private void createSpace(Class<? extends Annotation> spaceName, Properties properties) {
        final OsCacheDataGridImpl dataGrid = injector.getInstance(OsCacheDataGridImpl.class);
        final GeneralCacheAdministrator admin = new GeneralCacheAdministrator();

        //TODO set properties from cache.properties

        dataGrid.setAdmin(admin);

        register(spaceName, dataGrid);
    }
}
